package com.request;

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bdd.ConMySql;

public class BBJson2Sql implements JsonToSql{

	public BBJson2Sql() {
		super();
	}
	ConMySql con = new ConMySql("bddSite10001");
	
	public ArrayList<String> requestArray(JSONArray jsonA) throws JSONException{
		ArrayList<String>jsonList=new ArrayList<String>();
		for (int i = 0;i<jsonA.length();i++){
			JSONObject json = (JSONObject) jsonA.get(i);
			int productId = json.getInt("productId");	
			int productQty = json.getInt("productQty");
			//String sql = "update product set productQty = productQty-"+productQty+" where productId = "+productId;
			//String sql2 = "update product set bookedQty = bookedQty+"+productQty+" where productId = "+productId;
			//jsonList.add(sql);
			//jsonList.add(sql2);
		}
		return jsonList;
	}
	/**
	 * transfomrs a json object into a sql query
	 * tests if it's for a book or validate action
	 */
	public String requestObject(JSONObject jsonO) throws JSONException{
		String message = jsonO.getString("message");//type of action (book or valitate)
		int productId = jsonO.getInt("productId");
		int quantity = jsonO.getInt("productQty");
		String sql="Select * from product where productId = "+productId+";";
		Sql2Json s2j = new Sql2Json();
		JSONObject jTemp = s2j.responseObject(con.select(sql));// temp json for testing if there're enought products on hand
		
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% booking
		if(message.equals("resa")){
			if(jTemp.getInt("productQty")<=0){//if no product, do nothing
				
			}
			//if wanted qty more bookedQty less than productQty
			else if(jTemp.getInt("productQty")>0 && jTemp.getInt("bookedQty")+quantity<=jTemp.getInt("productQty")){
				String sql1 = "update product set bookedQty=bookedQty+"+quantity+" where productId = "+productId+" ;";
				con.execute(sql1);
			}
			//if desired qty 
			else if(jTemp.getInt("productQty")>0 && quantity>jTemp.getInt("productQty")){
				String sql1 = "update product set bookedQty=bookedQty+"+jTemp.getInt("productQty")+" where productId = "+productId+" ;";
				con.execute(sql1);
			}
		}
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% buying
		if(message.equals("validate")){
			if(jTemp.getInt("productQty")<=0){//si pas de produit, on ne fait rien
				
			}
			//si la quantité demandée + bookedQty est inferieure à la qte en stock
			else if(jTemp.getInt("productQty")>0 && jTemp.getInt("bookedQty")+quantity<=jTemp.getInt("productQty")){
				String sql1 = "update product set bookedQty=bookedQty-"+quantity+" where productId = "+productId+" ;";
				String sql2 = "update product set productQty=productQty-"+quantity+" where productId = "+productId+" ;";
				con.execute(sql1);
				con.execute(sql2);
			}
			//si la quantité demandée est sup à la qte en stock
			else if(jTemp.getInt("productQty")>0 && quantity>jTemp.getInt("productQty")){
				String sql1 = "update product set bookedQty=bookedQty-"+jTemp.getInt("productQty")+" where productId = "+productId+" ;";
				String sql2 = "update product set productQty=productQty-"+jTemp.getInt("productQty")+" where productId = "+productId+" ;";
				con.execute(sql2);
				con.execute(sql1);
			}
		}
		return sql;
	}
}
